﻿using FPC.Offer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using FPC.Offer.Model;
namespace FPC.Offer.UI.GeneralProcessor.RoleRight
{
    /// <summary>
    /// HandlerRoleRight 的摘要说明
    /// </summary>
    public class HandlerRoleRight : IHttpHandler
    {
        Model.System_RoleRight RoleRight = new Model.System_RoleRight();
        FPC.Offer.Service.System_RoleRightService RR = new Service.System_RoleRightService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var Actioon = context.Request["Actioon"].ToString();   
 
            if (Actioon == EnumAction.Select.ToString())
            {           
                    RoleRight_Model RoleRight_model = new RoleRight_Model();
                    FPC.Offer.Service.System_PowerTableService Power = new Service.System_PowerTableService();
                    var RoleId = Convert.ToInt32(context.Request["RoleId"].ToString());
                    var RoleRightList = RR.Find(t => t.RoleId == RoleId).ToList();
                    var PowerList = Power.Find(null).ToList();
                    foreach (var item in PowerList)
                    {
                        var GetList = RoleRightList.Where(p => p.PowerId == item.PowerId).FirstOrDefault();
                        if (GetList != null)
                        {
                            if (item.PowerSublevelId == 0)
                            {
                                if (item.PowerId == GetList.PowerId)
                                {
                                    RoleRight_model.RolePowerId += "    <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\"  name=\"PowerId\" type='checkbox' checked='checked'   class='check-box'/>" + item.PowerName + "";
                                }
                                else
                                {
                                    RoleRight_model.RolePowerId += "    <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\"  name=\"PowerId\" type='checkbox' class='check-box'/>" + item.PowerName + "";
                                }
                            }
                            else
                            {
                                if (item.PowerId == GetList.PowerId)
                                {
                                    RoleRight_model.RolePowerSublevelId += "  <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\" name=\"PowerId\" type='checkbox' checked='checked'  class='check-box'/>" + item.PowerName + "";
                                }
                                else
                                {
                                    RoleRight_model.RolePowerSublevelId += "    <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\"  name=\"PowerId\" type='checkbox' class='check-box'/>" + item.PowerName + "";
                                }
                            }
                        }
                        else
                        {
                            if (item.PowerSublevelId == 0)
                            {
                                RoleRight_model.RolePowerId += "    <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\" type='checkbox'  name=\"PowerId\" class='check-box'/>" + item.PowerName + "";
                            }
                            else
                            {
                                RoleRight_model.RolePowerSublevelId += "    <input id=\"Power" + item.PowerId + "\" value=\"" + item.PowerId + "\" type='checkbox'  name=\"PowerId\" class='check-box'/>" + item.PowerName + "";
                            }
                        }
                    }
                    context.Response.Write(JsonConvert.SerializeObject(RoleRight_model));               
            }
            //添加
            if (Actioon == EnumAction.Add.ToString())
            {
                context.Response.Write("");
            }
            //修改
            if (Actioon == EnumAction.Update.ToString())
            {
                context.Response.Write("");            
            }
            //删除
            if (Actioon == EnumAction.Delete.ToString())
            {               
                context.Response.Write(RR.RoleBatchUpdate(Convert.ToInt32(context.Request["GetId"]), context.Request["AddPowerId"].ToString()));  
            }

            context.Response.Write("");
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }      
    }
}